<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="活动名称" prop="title">
      <el-input v-model="dataForm.title" placeholder="活动名称"></el-input>
    </el-form-item>
    <el-form-item label="活动图片" prop="images">
      <el-input v-model="dataForm.images" placeholder="活动图片"></el-input>
    </el-form-item>
    <el-form-item label="描述" prop="descs">
      <el-input v-model="dataForm.descs" placeholder="描述"></el-input>
    </el-form-item>
    <el-form-item label="活动开始时间" prop="activeTime">
      <el-input v-model="dataForm.activeTime" placeholder="活动开始时间"></el-input>
    </el-form-item>
    <el-form-item label="活动结束时间" prop="deleteTime">
      <el-input v-model="dataForm.deleteTime" placeholder="活动结束时间"></el-input>
    </el-form-item>
    <el-form-item label="一等奖" prop="firstPrize">
      <el-input v-model="dataForm.firstPrize" placeholder="一等奖"></el-input>
    </el-form-item>
    <el-form-item label="二等奖" prop="secondPrize">
      <el-input v-model="dataForm.secondPrize" placeholder="二等奖"></el-input>
    </el-form-item>
    <el-form-item label="三等奖" prop="thiredPrize">
      <el-input v-model="dataForm.thiredPrize" placeholder="三等奖"></el-input>
    </el-form-item>
    <el-form-item label="一等奖人数" prop="firstPeople">
      <el-input v-model="dataForm.firstPeople" placeholder="一等奖人数"></el-input>
    </el-form-item>
    <el-form-item label="二等奖人数" prop="secondPeople">
      <el-input v-model="dataForm.secondPeople" placeholder="二等奖人数"></el-input>
    </el-form-item>
    <el-form-item label="三等奖人数" prop="thirdPoeple">
      <el-input v-model="dataForm.thirdPoeple" placeholder="三等奖人数"></el-input>
    </el-form-item>
    <el-form-item label="参与奖" prop="participationAward">
      <el-input v-model="dataForm.participationAward" placeholder="参与奖"></el-input>
    </el-form-item>
    <el-form-item label="参与奖个数" prop="participationPeople">
      <el-input v-model="dataForm.participationPeople" placeholder="参与奖个数"></el-input>
    </el-form-item>
    <el-form-item label="创建人" prop="createBy">
      <el-input v-model="dataForm.createBy" placeholder="创建人"></el-input>
    </el-form-item>
    <el-form-item label="修改人" prop="updateBy">
      <el-input v-model="dataForm.updateBy" placeholder="修改人"></el-input>
    </el-form-item>
    <el-form-item label="创建时间" prop="createDate">
      <el-input v-model="dataForm.createDate" placeholder="创建时间"></el-input>
    </el-form-item>
    <el-form-item label="更新时间" prop="updateDate">
      <el-input v-model="dataForm.updateDate" placeholder="更新时间"></el-input>
    </el-form-item>
    <el-form-item label="是否删除" prop="isDelete">
      <el-input v-model="dataForm.isDelete" placeholder="是否删除"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          title: '',
          images: '',
          descs: '',
          activeTime: '',
          deleteTime: '',
          firstPrize: '',
          secondPrize: '',
          thiredPrize: '',
          firstPeople: '',
          secondPeople: '',
          thirdPoeple: '',
          participationAward: '',
          participationPeople: '',
          createBy: '',
          updateBy: '',
          createDate: '',
          updateDate: '',
          isDelete: ''
        },
        dataRule: {
          title: [
            { required: true, message: '活动名称不能为空', trigger: 'blur' }
          ],
          images: [
            { required: true, message: '活动图片不能为空', trigger: 'blur' }
          ],
          descs: [
            { required: true, message: '描述不能为空', trigger: 'blur' }
          ],
          activeTime: [
            { required: true, message: '活动开始时间不能为空', trigger: 'blur' }
          ],
          deleteTime: [
            { required: true, message: '活动结束时间不能为空', trigger: 'blur' }
          ],
          firstPrize: [
            { required: true, message: '一等奖不能为空', trigger: 'blur' }
          ],
          secondPrize: [
            { required: true, message: '二等奖不能为空', trigger: 'blur' }
          ],
          thiredPrize: [
            { required: true, message: '三等奖不能为空', trigger: 'blur' }
          ],
          firstPeople: [
            { required: true, message: '一等奖人数不能为空', trigger: 'blur' }
          ],
          secondPeople: [
            { required: true, message: '二等奖人数不能为空', trigger: 'blur' }
          ],
          thirdPoeple: [
            { required: true, message: '三等奖人数不能为空', trigger: 'blur' }
          ],
          participationAward: [
            { required: true, message: '参与奖不能为空', trigger: 'blur' }
          ],
          participationPeople: [
            { required: true, message: '参与奖个数不能为空', trigger: 'blur' }
          ],
          createBy: [
            { required: true, message: '创建人不能为空', trigger: 'blur' }
          ],
          updateBy: [
            { required: true, message: '修改人不能为空', trigger: 'blur' }
          ],
          createDate: [
            { required: true, message: '创建时间不能为空', trigger: 'blur' }
          ],
          updateDate: [
            { required: true, message: '更新时间不能为空', trigger: 'blur' }
          ],
          isDelete: [
            { required: true, message: '是否删除不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/activity/activity/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.title = data.activity.title
                this.dataForm.images = data.activity.images
                this.dataForm.descs = data.activity.descs
                this.dataForm.activeTime = data.activity.activeTime
                this.dataForm.deleteTime = data.activity.deleteTime
                this.dataForm.firstPrize = data.activity.firstPrize
                this.dataForm.secondPrize = data.activity.secondPrize
                this.dataForm.thiredPrize = data.activity.thiredPrize
                this.dataForm.firstPeople = data.activity.firstPeople
                this.dataForm.secondPeople = data.activity.secondPeople
                this.dataForm.thirdPoeple = data.activity.thirdPoeple
                this.dataForm.participationAward = data.activity.participationAward
                this.dataForm.participationPeople = data.activity.participationPeople
                this.dataForm.createBy = data.activity.createBy
                this.dataForm.updateBy = data.activity.updateBy
                this.dataForm.createDate = data.activity.createDate
                this.dataForm.updateDate = data.activity.updateDate
                this.dataForm.isDelete = data.activity.isDelete
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/activity/activity/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'title': this.dataForm.title,
                'images': this.dataForm.images,
                'descs': this.dataForm.descs,
                'activeTime': this.dataForm.activeTime,
                'deleteTime': this.dataForm.deleteTime,
                'firstPrize': this.dataForm.firstPrize,
                'secondPrize': this.dataForm.secondPrize,
                'thiredPrize': this.dataForm.thiredPrize,
                'firstPeople': this.dataForm.firstPeople,
                'secondPeople': this.dataForm.secondPeople,
                'thirdPoeple': this.dataForm.thirdPoeple,
                'participationAward': this.dataForm.participationAward,
                'participationPeople': this.dataForm.participationPeople,
                'createBy': this.dataForm.createBy,
                'updateBy': this.dataForm.updateBy,
                'createDate': this.dataForm.createDate,
                'updateDate': this.dataForm.updateDate,
                'isDelete': this.dataForm.isDelete
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
